//
//  GoogleRefreshHeader.m
//  T_TableViewDemo
//
//  Created by CRY on 16/12/1.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import "GoogleRefreshHeader.h"
#import "MDProgress.h"

#define GOOGLE_COLOR MJRefreshColor(144, 144, 144)
#define RANDIUS 36

typedef enum : NSUInteger {
    
    CirclrStatusBecomeSmall,
    CirclrStatusBecomeBig,
    CirclrStatusSpread,
    CirclrStatusSpringAnimation,
} CirclrStatus;

@interface GoogleRefreshHeader()

@property (nonatomic, strong) UIView *controlPoint;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) MDProgress *progress;

@end

@implementation GoogleRefreshHeader

- (UIView *)controlPoint{
    
    if (_controlPoint == nil) {
        
        _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        _backgroundView.layer.masksToBounds = YES;
        [self addSubview:_backgroundView];
        
        _controlPoint = [[UIView alloc] init];
        _controlPoint.mj_size = CGSizeMake(RANDIUS, RANDIUS);
        _controlPoint.backgroundColor = GOOGLE_COLOR;
        _controlPoint.transform = CGAffineTransformMakeScale(0.01, 0.01);
        _controlPoint.layer.cornerRadius = RANDIUS/2;
        [_backgroundView addSubview:_controlPoint];
        
        _progress = [[MDProgress alloc] initWithFrame:CGRectZero];
        _progress.mj_size = CGSizeMake(RANDIUS - 5, RANDIUS - 5);
        [_progress setTrackWidth:6];
        [_progress setProgressColor:GOOGLE_COLOR];
        [_progress setProgressStyle:MDProgressStyleCircular] ;
        [_progress setProgressType:MDProgressTypeIndeterminate];
        _progress.transform = CGAffineTransformMakeScale(0.6, 0.6);
        _progress.hidden = YES;
        [_backgroundView addSubview:_progress];
    }
    return _controlPoint;
}
#pragma mark - 重写父类方法
- (void)setState:(MJRefreshState)state{
    MJRefreshCheckState
    if (state == MJRefreshStateIdle) {
        
        if (oldState == MJRefreshStatePulling) {
            NSLog(@"/** 松开就可以进行刷新的状态 --> 闲置状态 */");
            [self setCircleStatus:CirclrStatusBecomeSmall];
        } else if (oldState == MJRefreshStateRefreshing){
            NSLog(@"/** 刷新的状态 --> 闲置状态 */");
            [self setCircleStatus:CirclrStatusSpringAnimation];
        }
    } else if (state == MJRefreshStatePulling) {
        NSLog(@"/** 松开就可以进行刷新的状态 */");
        [self setCircleStatus:CirclrStatusBecomeBig];
    } else if (state == MJRefreshStateRefreshing) {
        NSLog(@"/** 刷新的状态 */");
        [self setCircleStatus:CirclrStatusSpread];
        [_progress setProgressType:MDProgressTypeIndeterminate];
    }
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    
    if (self.scrollView.mj_offsetY > 0) {
        [super scrollViewContentOffsetDidChange:change];
        return;
    }
    self.backgroundView.mj_h = self.scrollView.mj_offsetY;
    self.backgroundView.mj_y = self.scrollView.mj_offsetY + self.mj_h;
    self.controlPoint.center = CGPointMake(self.mj_w / 2, self.backgroundView.mj_h / 2);
    self.progress.center = CGPointMake(self.mj_w / 2, self.backgroundView.mj_h / 2);
    [super scrollViewContentOffsetDidChange:change];
}

- (void)setPullingPercent:(CGFloat)pullingPercent{
    [super setPullingPercent:pullingPercent];
}
// 根据不同状态执行相应动作
- (void)setCircleStatus:(CirclrStatus)status{
   
    CGAffineTransform scale;
    float alpha;
    NSTimeInterval timeInterval = 0.3;
    if (status == CirclrStatusBecomeSmall) {
        scale = CGAffineTransformMakeScale(0.01, 0.01);
        alpha = 1;
        _progress.hidden = YES;
    }else if (status == CirclrStatusBecomeBig){
        scale = CGAffineTransformMakeScale(1, 1);
        alpha = 1;
        _progress.hidden = YES;
    }else if (status == CirclrStatusSpread){
        scale = CGAffineTransformMakeScale(20, 20);
        alpha = 0;
        _progress.hidden = NO;
    }else if (status == CirclrStatusSpringAnimation){
        scale = CGAffineTransformMakeScale(1, 1);
        alpha = 1;
        timeInterval = 0.15;
        _progress.hidden = YES;
    }
    
    [UIView animateWithDuration:timeInterval animations:^{
        self.controlPoint.transform = scale;
        self.controlPoint.alpha = alpha;
    } completion:^(BOOL finished) {
        if (status == CirclrStatusSpread) {
            self.controlPoint.transform = CGAffineTransformMakeScale(0.01, 0.01);
            self.alpha = 1;
        }else if (status == CirclrStatusSpringAnimation){
            _progress.hidden = YES;
            [UIView animateWithDuration:timeInterval animations:^{
                self.controlPoint.transform = CGAffineTransformMakeScale(0.01, 0.01);
                self.alpha = 1;
            }];
        }
    }];
}

@end
